package git.soulbgm.common.pojo;

import com.github.pagehelper.PageHelper;
import git.soulbgm.utils.ServletUtils;

/**
 * 表格数据处理
 *
 * @author SoulBGM
 * @date 2021/05/17
 */
public class TableSupport {

    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String SORTING = "sorting";

    /**
     * 封装分页对象
     */
    public static PageDomain buildPageRequest() {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(ServletUtils.getIntParameter(PAGE_NUM));
        pageDomain.setPageSize(ServletUtils.getIntParameter(PAGE_SIZE));
        pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
        pageDomain.setSorting(ServletUtils.getParameter(SORTING));
        return pageDomain;
    }

    public static void startPage() {
        PageDomain pageDomain = buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
    }


}
